package cn.tedu.carehome.foundation.service.impl;

import cn.tedu.carehome.foundation.ex.ServiceException;
import cn.tedu.carehome.foundation.mapper.BedMapper;
import cn.tedu.carehome.foundation.mapper.CheckInfoMapper;
import cn.tedu.carehome.foundation.pojo.dto.CheckInfoAddNewDto;
import cn.tedu.carehome.foundation.pojo.dto.CheckInfoUpdateDTO;
import cn.tedu.carehome.foundation.pojo.entity.CheckInfo;
import cn.tedu.carehome.foundation.pojo.vo.CheckInfoAllOlderAddInfoVo;
import cn.tedu.carehome.foundation.pojo.vo.CheckInfoListItemVO;
import cn.tedu.carehome.foundation.pojo.vo.CheckInfoStandardVo;
import cn.tedu.carehome.foundation.service.ICheckInfoService;
import cn.tedu.carehome.foundation.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class CheckInfoServiceImpl implements ICheckInfoService {
    @Autowired
    CheckInfoMapper checkInfoMapper;

    @Autowired
    BedMapper bedMapper;

    @Override
    public void deleteByid(Long id) {
        log.debug("开始处理【删除入住信息】列表业务！");
        if(checkInfoMapper.getStandarByid(id)==null)
        {
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"删除错误,非法此id!");
        }
        if(bedMapper.countByCheckId(id)>0)
        {
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"删除失败,改老人正在入住,请先退出该老人的床位！");
        }
        checkInfoMapper.deleteByid(id);
        log.debug("删除成功！");
    }

    @Override
    public void deleteByids(Long[] ids) {
        log.debug("开始处理【批量删除入住信息】列表业务！");
        if(bedMapper.countByCheckIds(ids)>0)
        {
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"删除失败,其中有老人正在入住,请先退出该老人的床位！");
        }
        checkInfoMapper.deleteBatchByids(ids);
    }

    @Override
    public List<CheckInfoListItemVO> list(String Content) {
        log.debug("开始处理查询【查询入住信息全部】业务");
        return checkInfoMapper.list(Content);
    }

    @Override
    public List<CheckInfoListItemVO> listLimit(int page,int size,String Content) {
        log.debug("开始处理【查询入住登记】列表业务！");
        return checkInfoMapper.listLimit(page,size,Content);
    }

    @Override
    public List<CheckInfoAllOlderAddInfoVo> listAllOlderInfo(String Content)
    {
        return checkInfoMapper.listAllOlderInfo(Content);
    }

    @Override
    public CheckInfoStandardVo checkByid(Long id) {
       return checkInfoMapper.getStandarByid(id);
    }

    @Override
    public int count(String Content) {
        return checkInfoMapper.count(Content);
    }

    @Override
    public void addNew(CheckInfoAddNewDto checkInfoAddNewDto) {
        if(checkInfoMapper.countByOlderId(checkInfoAddNewDto.getOlderId())>0)
        {
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"该老人入住登记信息已存在！");
        }
        CheckInfo  checkInfo = new CheckInfo();
        BeanUtils.copyProperties(checkInfoAddNewDto,checkInfo);
        checkInfo.setGmtCreate(LocalDateTime.now());
        checkInfo.setGmtModified(LocalDateTime.now());
        checkInfoMapper.insert(checkInfo);
    }

    @Override
    public void update(CheckInfoUpdateDTO checkInfoUpdateDTO) {
        if(checkInfoMapper.countByOlderIdNotId(checkInfoUpdateDTO.getId(),checkInfoUpdateDTO.getOlderId())>0)
        {
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"修改失败,该老人入住登记信息已存在!");
        }
        checkInfoMapper.update(checkInfoUpdateDTO);
    }

}
